package cn.tedu.lucky.draw.server.service.impl;

import cn.tedu.lucky.draw.server.protocol.po.Rule;
import cn.tedu.lucky.draw.server.protocol.query.RuleQuery;
import cn.tedu.lucky.draw.server.repository.RuleRepository;
import cn.tedu.lucky.draw.server.service.IRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RuleServiceImpl implements IRuleService {
    @Autowired
    private RuleRepository ruleRepository;

    private List<Rule> rules;
    @Override
    public List<Rule> findAll(RuleQuery query) {
        if (rules == null) {
            rules = ruleRepository.findAll();
        }
        String name = query.getName();
        Integer status = query.getStatus();
        return rules.stream().filter(rule -> (name == null || rule.getName().equals(name) || name == ""))
                .filter(rule -> (status == null || rule.getStatus().equals(status))).collect(Collectors.toList());
    }
}
